package cn.kgc.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.mapper.AdminMapper;
import cn.kgc.entity.Admin;
import cn.kgc.service.AdminService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * @author k
 */
@Slf4j
@Service("adminService")
@SuppressWarnings("all")
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Admin adminLogin(String username, String password) {
        //根据账号查询用户的信息
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,username);
        Admin admin= getBaseMapper().selectOne(queryWrapper);
        // //如果用户不为空,对比密码是否正确,密码正确返回用户的信息（清除数据库中查询的密码信息）
        if( (admin!=null) && (BCrypt.checkpw(password,admin.getPassword()))){
            admin.setPassword("");
            return admin;
        }
        //如果用户为空或者密码错误直接返回null
        return null;
    }
}
